package com.example.wechat;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Fragment1 extends Fragment {
    RecyclerView recyclerView;
    List list;
    Myadapter adapter;
    Context context;
    View view;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.tab1,container,false);
        recyclerView=view.findViewById(R.id.recyclerview);//连接至tab1的控件
        //创建数据
        String[] names={"老爸","老妈","大姐","小弟","东东","大方","老杨","老师","二叔","大爷"};
        int[] images={R.drawable.baba,R.drawable.mama,R.drawable.jiejie, R.drawable.didi,R.drawable.xixi,
                R.drawable.xiaofang, R.drawable.xiaoyang,R.drawable.teacher,R.drawable.shushu,R.drawable.yeye};
        String[] phones={"13412345678","13466666666","18914145140","13777788899","13699999999",
                "17288888888","17333333333","13444444444","13617777777","18987654321"};
        String[] regions={"湖北 武汉","东北 铁岭","四川 成都","百慕大 三角","福建 福州","湖北 武汉","湖北 武汉",
                "福建 福州","福建 福州","福建 福州"};
        List<Map<String,Object>> items=new ArrayList<Map<String,Object>>();
        for(int i=0;i<names.length;i++){
            Map<String,Object> item=new HashMap<String, Object>();
            item.put("i_name",names[i]);
            item.put("i_image",images[i]);
            item.put("i_phone",phones[i]);
            item.put("i_region",regions[i]);
            items.add(item);
        }

        //创建RecycleView实例和设置Adapter
        Context context=getContext();
        adapter=new Myadapter(view.getContext(),items);//把list导入
        LinearLayoutManager manager=new LinearLayoutManager(view.getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(manager);
        // Inflate the layout for this fragment

    //实现拖拽和左滑删除效果
    ItemTouchHelper itemTouchHelper=new ItemTouchHelper(new ItemTouchHelper.Callback() {
        @Override
        public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
            int swiped=ItemTouchHelper.LEFT;
            int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
            return makeMovementFlags(dragFlags,swiped);
        }

        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
            int oldPosition = viewHolder.getAdapterPosition();
            int newPosition = target.getAdapterPosition();
            if (oldPosition < newPosition) {
                for (int i = oldPosition; i < newPosition; i++) {
                    // 改变数据集
                    Collections.swap(items, i, i +1);
                }
            } else {
                for (int i = oldPosition; i > newPosition; i--) {
                    // 改变数据集
                    Collections.swap(items, i, i - 1);
                }
            }
            adapter.notifyItemMoved(oldPosition, newPosition);
            return true;
        }

        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
            int position = viewHolder.getAdapterPosition();
            items.remove(position);
           adapter.notifyItemRemoved(position);
        }
        @Override
        public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
            if (actionState!=ItemTouchHelper.ACTION_STATE_IDLE){
                viewHolder.itemView.setBackgroundColor(Color.parseColor("#04BE02"));
            }
        }
        @Override
        public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            super.clearView(recyclerView, viewHolder);
            viewHolder.itemView.setBackgroundColor(Color.TRANSPARENT);
        }
    });
    //关联recycleView
        itemTouchHelper.attachToRecyclerView(recyclerView);
        return view;
}

}